package com.tx.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 */
public class JedisDemo {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.auth("123456");
        System.out.println(jedis.ping());
        //1、String
        /*System.out.println("======1、String========");
        testString(jedis);*/

        //2、List
//        testList(jedis);

        //3、Set
//        testSet(jedis);


        //4、Hash
//        testHash(jedis);


        //5、ZSet
        testZSet(jedis);
        //0、key键
        System.out.println("==============");
        Set<String> keys = jedis.keys("*");
        keys.stream().forEach(System.out::println);


        //设置1w条数据
//        testSetData(jedis);
    }

    private static void testSetData(Jedis jedis) {
        for (int i = 0; i < 10000; i++) {
            jedis.set("k" + i, "v" + i);
        }
    }


    private static void testZSet(Jedis jedis) {
        jedis.flushDB();
        Map<String,Double> map = new HashMap();
        map.put("v1",10d);
        map.put("v2",20d);
        jedis.zadd("k1",map);
        jedis.zrange("k1",0,1).forEach(System.out::println);
    }

    private static void testHash(Jedis jedis) {
        jedis.flushDB();
        jedis.hset("user","name","mercury");
        System.out.println(jedis.hget("user", "name"));

        Map map = new HashMap();
        map.put("name","mercury");
        map.put("age","18");
        jedis.hmset("customer",map);
        List<String> result = jedis.hmget("customer", "name", "age");
        result.stream().forEach(System.out::println);
    }

    private static void testSet(Jedis jedis) {
        jedis.flushDB();
        //3.1 sadd/smembers/sismember
        jedis.sadd("k1","1","1","2","2","3","5");
        jedis.smembers("k1").forEach(System.out::println);
        System.out.println(jedis.sismember("k1", "1"));

        //3.2 scard key  --》查看set的长度
        System.out.println(jedis.scard("k1"));

        // 3.3 srem key value
        jedis.srem("k1","1");
        jedis.smembers("k1").forEach(System.out::println);

        //3.4 srandmember key
        System.out.println(jedis.srandmember("k1",2));

        //3.5 spop key
        System.out.println(jedis.spop("k1"));

        //3.6 smove key1 key2 在key1里某个值
        jedis.smove("k1","k2","2");
        System.out.print("k2:");
        jedis.smembers("k2").forEach(System.out::println);
        System.out.print("k1:");
        jedis.smembers("k1").forEach(System.out::println);

        //3.7 数学集合类
        jedis.sdiff("k1","k2").forEach(System.out::println);
        jedis.sunion("k1","k2").forEach(System.out::println);
        jedis.sinter("k1","k2").forEach(System.out::println);
    }

    //2.测试list
    private static void testList(Jedis jedis) {
        //清空当前redis库，方便后面测试
        jedis.flushDB();

        System.out.println("======2、List========");
        //2.1 lpush,rpush,lrange
        jedis.lpush("k1","1","2","3","4","5");
        System.out.println(jedis.lrange("k1",0,2));

        jedis.rpush("k2","1","2","3","4","5","6");
        System.out.println(jedis.lrange("k2",1,2));


        //2.2 lpop/rpop
        System.out.println(jedis.lpop("k1"));
        System.out.println(jedis.rpop("k2"));

        //2.3  lindex，按照索引下标获得元素(从上到下)
        System.out.println("index=0，value:" + jedis.lindex("k1", 0));

        //2.4  llen
        System.out.println(jedis.llen("k2"));

        //2.5 lrem key count value: 删key中count个value
        jedis.lrem("k2",3,"1");

        //2.6  ltrim key 开始index 结束index，截取指定范围的值后再赋值给key
        jedis.ltrim("k2",0,2);
        System.out.println(jedis.lrange("k2",0,-1));

        //2.7 rpoplpush 源列表 目的列表
        jedis.rpoplpush("k1","k2");
        System.out.println(jedis.lrange("k2",0,-1));

        //2.8 lset key index value
        jedis.lset("k2",1,"hh");
        System.out.println(jedis.lrange("k2",0,-1));

        //2.9 linsert key before/after 值1 值2
        jedis.linsert("k2", ListPosition.BEFORE,"3","world");
        System.out.println(jedis.lrange("k2",0,-1));
    }


    //1、测试String
    private static void testString(Jedis jedis) {
        //清空当前redis库，方便后面测试
        jedis.flushDB();


        jedis.set("k1","v1");
        String result = jedis.get("k1");
        System.out.println(result);

        jedis.set("num","1");
        jedis.incr("num");
        System.out.println(jedis.get("num"));

        jedis.set("k2","hello redis");
        System.out.println(jedis.getrange("k2",1,3));

        //setex:key存在才设置
        jedis.setex("k2",10,"vv2");

        //setnx: key不存在才设置成功
        jedis.setnx("k1","kv2"); //k1 存在，设置失败

        //mset,mget
        jedis.mset("k3","v3","k4","v4");
        List<String> returnValues = jedis.mget("k3", "k4");
        returnValues.forEach(System.out::println);

        String s = jedis.getSet("k3", "vv3");
        System.out.println("k3的旧值：" +s);


    }
}